package datacontainers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import application.UserData;

public class Order {

	private List<OrderItem> orderItems = new ArrayList<OrderItem>();

	private User user = new User();
	private Date date;

	public Order() {
		// TODO load from settings
		user.setName("UserName");
		user.setPcName(UserData.getPCName());
		date = new Date();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User u) {
		user = u;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<OrderItem> getItems() {
		return orderItems;
	}

	public void setItems(List<OrderItem> items) {
		this.orderItems = items;
	}

	private OrderItem findItem(Dish dish) {
		for (OrderItem item : orderItems) {
			if (item.getDish().equals(dish)) {
				return item;
			}
		}
		return null;
	}

	public void setDishCount(Dish dish, int count) {
		OrderItem item = findItem(dish);
		if (item != null) {
			item.setCount(count);
		} else {
			orderItems.add(new OrderItem(dish, count));
		}
	}

	public int getDishCount(Dish dish) {
		OrderItem item = findItem(dish);
		if (item != null) {
			return item.getCount();
		} else {
			return 0;
		}
	}

	public void deleteDish(Dish dish) {
		orderItems.remove(findItem(dish));
	}

}
